{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "scputimes(user=14341.484375, system=31892.640625, idle=253024.328125, interrupt=612.9531240463257, dpc=484.04687666893005)\n",
      "14341.484375\n",
      "[scputimes(user=1237.671875, system=4036.1875, idle=19665.515625, interrupt=456.84375, dpc=432.859375), scputimes(user=853.515625, system=2671.65625, idle=21412.9375, interrupt=19.546875, dpc=12.6875), scputimes(user=1475.421875, system=3457.0, idle=20005.6875, interrupt=18.0625, dpc=5.515625), scputimes(user=1069.65625, system=1860.515625, idle=22007.9375, interrupt=10.046875, dpc=3.484375238418579), scputimes(user=1105.8125, system=2307.3125, idle=21524.984375, interrupt=15.40625, dpc=4.09375), scputimes(user=797.40625, system=1598.765625, idle=22541.921875, interrupt=9.015625, dpc=2.5625), scputimes(user=1216.234375, system=2701.328125, idle=21020.53125, interrupt=14.062499046325684, dpc=3.859375238418579), scputimes(user=1294.15625, system=2924.171875, idle=20719.765625, interrupt=11.765625, dpc=2.953125), scputimes(user=1331.984375, system=2868.234375, idle=20737.875, interrupt=16.15625, dpc=4.046875476837158), scputimes(user=1530.375, system=2701.765625, idle=20705.953125, interrupt=12.828125, dpc=4.078125), scputimes(user=1506.59375, system=3070.40625, idle=20361.078125, interrupt=20.171875, dpc=4.359375476837158), scputimes(user=922.65625, system=1695.28125, idle=22320.140625, interrupt=9.046875, dpc=3.546875238418579)]\n",
      "12\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "import psutil\n",
    "# cpu信息\n",
    "\n",
    "print(psutil.cpu_times())\n",
    "print(psutil.cpu_times().user)\n",
    "print(psutil.cpu_times(percpu=True))\n",
    "print(psutil.cpu_count())\n",
    "print(psutil.cpu_count(logical=False))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "svmem(total=17032671232, available=7689572352, percent=54.9, used=9343098880, free=7689572352)\n",
      "\n",
      "17032671232\n",
      "7689576448\n",
      "sswap(total=17032671232, used=10900594688, free=6132076544, percent=64.0, sin=0, sout=0)\n"
     ]
    }
   ],
   "source": [
    "# 内存信息\n",
    "import psutil\n",
    "print(psutil.virtual_memory())\n",
    "print()\n",
    "print(psutil.virtual_memory().total)\n",
    "print(psutil.virtual_memory().free)\n",
    "print(psutil.swap_memory())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[sdiskpart(device='C:\\\\', mountpoint='C:\\\\', fstype='NTFS', opts='rw,fixed'), sdiskpart(device='D:\\\\', mountpoint='D:\\\\', fstype='NTFS', opts='rw,fixed'), sdiskpart(device='E:\\\\', mountpoint='E:\\\\', fstype='NTFS', opts='rw,fixed')]\n",
      "\n",
      "sdiskusage(total=255330144256, used=86329270272, free=169000873984, percent=33.8)\n",
      "sdiskio(read_count=383575, write_count=622978, read_bytes=20437633536, write_bytes=19317652992, read_time=413, write_time=766)\n",
      "{'PhysicalDrive0': sdiskio(read_count=33128, write_count=97344, read_bytes=3099861504, write_bytes=3454506496, read_time=10, write_time=25), 'PhysicalDrive1': sdiskio(read_count=331829, write_count=524894, read_bytes=16714242048, write_bytes=15698278400, read_time=356, write_time=731), 'PhysicalDrive2': sdiskio(read_count=18618, write_count=740, read_bytes=623529984, write_bytes=164868096, read_time=47, write_time=10)}\n"
     ]
    }
   ],
   "source": [
    "#磁盘信息\n",
    "import psutil\n",
    "\n",
    "print(psutil.disk_partitions())\n",
    "print()\n",
    "print(psutil.disk_usage('/'))\n",
    "print(psutil.disk_io_counters())\n",
    "print(psutil.disk_io_counters(perdisk=True))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "snetio(bytes_sent=95272147, bytes_recv=2235950654, packets_sent=377306, packets_recv=1757511, errin=0, errout=3, dropin=0, dropout=0)\n",
      "\n",
      "{'以太网': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), '以太网 2': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), '本地连接* 2': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'VMware Network Adapter VMnet1': snetio(bytes_sent=5882, bytes_recv=58, packets_sent=5881, packets_recv=58, errin=0, errout=0, dropin=0, dropout=0), 'VMware Network Adapter VMnet8': snetio(bytes_sent=14442, bytes_recv=58, packets_sent=14442, packets_recv=58, errin=0, errout=0, dropin=0, dropout=0), '以太网 4': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'WLAN': snetio(bytes_sent=95192389, bytes_recv=2235945674, packets_sent=356540, packets_recv=1757363, errin=0, errout=0, dropin=0, dropout=0), '蓝牙网络连接': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'Loopback Pseudo-Interface 1': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), '本地连接* 4': snetio(bytes_sent=59434, bytes_recv=4864, packets_sent=443, packets_recv=32, errin=0, errout=3, dropin=0, dropout=0), 'isatap.{026CCF7B-71A6-44CF-82EC-4FC567660B95}': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'isatap.{C4BC60F4-1E02-4C55-8614-1913EB35FE31}': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'isatap.{C05F6ED6-2FC2-4E97-B9A6-6F8584FFC50F}': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0)}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "#网络信息\n",
    "import psutil\n",
    "\n",
    "temp = psutil.net_io_counters()\n",
    "print(temp)\n",
    "print()\n",
    "print(psutil.net_io_counters(pernic=True))\n",
    "print()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[suser(name='V5', terminal=None, host='0.4.0.0', started=1542510378.0, pid=None)]\n",
      "1542510362.0\n",
      "2018-11-18 11:06:02\n"
     ]
    }
   ],
   "source": [
    "#其他系统信息\n",
    "import psutil\n",
    "import datetime\n",
    "\n",
    "print(psutil.users())\n",
    "print(psutil.boot_time())\n",
    "print(datetime.datetime.fromtimestamp(psutil.boot_time()).strftime(\"%Y-%m-%d %H:%M:%S\"))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "conhost.exe C:\\Windows\\System32\\conhost.exe\n",
      "C:\\WINDOWS running 1542536824.0 pcputimes(user=0.015625, system=0.03125, children_user=0.0, children_system=0.0) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] 0.05790734680224233 pmem(rss=9863168, vms=5173248, num_page_faults=2492, peak_wset=9863168, wset=9863168, peak_paged_pool=164848, paged_pool=164624, peak_nonpaged_pool=7864, nonpaged_pool=7864, pagefile=5173248, peak_pagefile=5173248, private=5173248) pio(read_count=0, write_count=0, read_bytes=0, write_bytes=0, other_count=402, other_bytes=870) [] 4\n"
     ]
    }
   ],
   "source": [
    "#进程\n",
    "import psutil\n",
    "\n",
    "pid = psutil.pids()[-1]\n",
    "p = psutil.Process(pid)\n",
    "print(p.name(),p.exe())\n",
    "print(p.cwd(),p.status(),p.create_time(),p.cpu_times(),p.cpu_affinity(),p.memory_percent(),p.memory_info(),p.io_counters(),p.connections(),p.num_threads())\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "python.exe\n",
      "DESKTOP-U8OTTDB\\V5\n",
      "(b'hello,popen\\r\\n', None)\n"
     ]
    }
   ],
   "source": [
    "# popen类的使用\n",
    "import psutil\n",
    "from subprocess import PIPE\n",
    "\n",
    "p = psutil.Popen([\"python\", \"-c\", \"print('hello,popen')\"], stdout=PIPE)\n",
    "print(p.name())\n",
    "print(p.username())\n",
    "print(p.communicate())\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "file_extension": ".py",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  },
  "mimetype": "text/x-python",
  "name": "python",
  "npconvert_exporter": "python",
  "pygments_lexer": "ipythonP",
  "version": "Python 3.6.4 :: Anaconda, Inc."
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
